#!/bin/bash
#    am-conky-pipemenu - an Openbox pipemenu for manipulating conky
#    Copyright (C) 2015 damo <damo@bunsenlabs.org>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

HELP='    am-conky-pipemenu an Openbox Pipe Menu for use with conky
    It should normally be called from an openbox menu.

Options:
    -h --help   show this message

    If am-conky-pipemenu is called with no options (the usual method),
    it will output an xml openbox pipemenu for handling conky sessions.

See /usr/share/arcolinux/docs/helpfile-conkyzen.txt and "man conky"
for more information.
'
# look for a help option somewhere
for i in "$@"
do
    case "$i" in
    -h|--help)
        echo "$HELP"
        exit 0
        ;;
    esac
done

AL_COMMON_LIBDIR='/usr/lib/arcolinux/common'

CONKYDEFAULT="$HOME/.conkyrc"
CONKYPATH="$HOME/.config/conky"
CSESSIONFILE="$CONKYPATH/conky-sessionfile"
SESSIONS="$CONKYPATH/saved-sessions"
CZEN="am-conkyzen"
CSESSION="am-conky-session"
HELPFILE="/usr/share/arcolinux/docs/helpfile-conkyzen.txt"

if ! . "$AL_COMMON_LIBDIR/am-include.cfg" 2> /dev/null; then
    echo "Error: Failed to source am-include.cfg in $AL_COMMON_LIBDIR" >&2
    exit 1
fi
if [ ! -e "$CSESSIONFILE" ] 2> /dev/null ; then
    echo "Error: Failed to locate conky-sessionfile in $CONKYPATH" >&2
    echo "conky -c $HOME/.conkyrc & sleep 1" > "$CSESSIONFILE"
fi
if [ ! -e "$SESSIONS" ] 2> /dev/null ; then
    echo "$0: Failed to locate saved-sessions in $CONKYPATH" >&2
fi

declareDependencies yad

getSessions(){  # read any saved sessions from save-sessions file
    menuItem "Default" "$CSESSION $CSESSIONFILE"
    if [ -e "$SESSIONS" ]
    then
        while read -r session ;do
            [[ -z $session ]] && continue   # skip if line is blank
            menuItem "$session" "$CSESSION $session"
        done < "$SESSIONS"
    else
        echo "$0: Failed to locate saved-sessions in $CONKYPATH" >&2
    fi
}

loadChooserMenu(){
    if type "$CZEN" &>/dev/null;then  # am-conkyzen script found
        menuItem "Conkyzen" "$CZEN"
    fi
}

loadNewsessionMenu(){
    if type "$CZEN" &>/dev/null;then  # am-conkyzen script found
        menuItem "New Conky Session" "$CZEN -z"
    fi
}

loadReloadMenu(){
    if type "$CSESSION" &>/dev/null;then  # am-conky-session script found
        menuItem "Reload Conkys" "$CSESSION"
    fi
}

loadSavedsessionSubmenu(){
    if type "$CSESSION" &>/dev/null;then  # am-conky-session script found
        menuSubmenu "Sessions" "Saved Sessions"
        getSessions
        menuSubmenuEnd
    fi
}

# loadPinMenu(){
#     if type am-conkypin &>/dev/null;then
#         menuItem "Pin moveable Conkys" "am-conkypin"
#         menuSeparator
#     fi
# }

loadEditMenu(){
    #menuItem "Default conkyrc" "geany $CONKYDEFAULT"
    if [[ $(pidof conky) ]];then
        menuSubmenu "RunningConky" "Running Conkys"
        while read -r session ;do  # get running conkys from sessionfile
            [[ -z $session ]] && continue
            CPATH=$(echo "$session" | awk '{print $3}')
            CONKY=$(echo "$CPATH" | awk -F"/" '{print $(NF-1)"/"$NF }')
            menuItem "$CONKY" "geany $CPATH"
        done < "$CSESSIONFILE"
        menuSubmenuEnd
    else
        menuItem "No Running Conkys"
    fi
}

getConkys(){
    menuSubmenu "EditConky" "Edit Conkys"
        if type am-conkyedit &>/dev/null;then  # am-conkyedit script found
            menuItem "Conky Editor" "am-conkyedit"
            loadEditMenu
        else
            loadEditMenu
        fi
    menuSubmenuEnd
}

checkHelpfile(){
    DLG="yad --text-info --center --borders=20 \
    --title='ArcoLinux Conky Manager Help' \
    --filename=$HELPFILE --fontname=Monospace \
    --width=650 --height=700 \
    --button=yad-ok:0"
    if [[ -e $HELPFILE ]];then
        menuItem "Conky Manager Help" "$DLG"
    fi
}

menuStart "Conkymenu" "Conky"
loadChooserMenu
loadReloadMenu
getConkys
menuSeparator
#loadPinMenu
loadNewsessionMenu
loadSavedsessionSubmenu
menuSeparator
checkHelpfile
menuEnd
